package com.heima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.*;
import com.heima.pojo.dto.NodeQueryParamDto;
import com.heima.pojo.dto.RegionQueryParamDto;
import com.heima.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageBean getRegionList(RegionQueryParamDto areaDto) {
        PageHelper.startPage(areaDto.getPageIndex(), areaDto.getPageSize());
        List<Region> list = regionMapper.getRegionList(areaDto);
        PageInfo<Region> areaPageInfo = new PageInfo<>(list);
        log.info("拿到的 集合是{}",areaPageInfo.getList());
        log.info("总条数是：{}{}",areaPageInfo.getPages(),areaPageInfo.getTotal());
        return new PageBean(
                areaDto.getPageIndex(),
                areaDto.getPageSize(),
                (long) areaPageInfo.getPages(),
                areaPageInfo.getTotal(),
                areaPageInfo.getList()
        );
    }

    @Override
    public Boolean addRegion(Region2 region) {
        if (regionMapper.selectRegionByNameReturn1(region.getRegionName()) != null){
            return false;
        }
        regionMapper.addRegion(region);
        return  true;
    }

    @Override
    public Boolean updateRegion(Long regionId, Region2 region2) {
        //拿到id对应的名字 如果查到的数量不是空，更新失败
        String name = regionMapper.selectRegionByName(region2.getRegionName(),regionId);
        log.info("s 的内容是:{}",name);
        if (name != null){
            return false;
        }
        region2.setId(regionId);
        regionMapper.updateRegion(region2);
        return true;
    }

    @Override
    public PageBean selectNode(NodeQueryParamDto nodeDto) {
        PageHelper.startPage(nodeDto.getPageIndex(),nodeDto.getPageSize());
        List<Node> list =regionMapper.getNodeList(nodeDto);
        log.info("查询结果list:{}",list.size());

        PageInfo<Node> pageInfo = new PageInfo<>(list);

        for (Node node : list) {
            Region region = regionMapper.selectRegionByRegionId2(node.getRegionId());
            log.info("region到底有多少个！？{}",region);
            //通过regionId拿到设备数量
            Long s = regionMapper.selectVMCount(nodeDto.getRegionId());
            //添加进node中 ， 把region加进去
            node.setVmCount(s);
            node.setRegion(region);
            log.info("设备数量为：{}",node.getVmCount());

            node.setBusinessType(regionMapper.selectBusinessTypefromNode(node.getId()));
        }

        PageBean pageBean = new PageBean(nodeDto.getPageIndex(), nodeDto.getPageSize(), (long) pageInfo.getPages(), pageInfo.getTotal(), pageInfo.getList());

        return pageBean;
    }

    @Override
    public Boolean deleteRegion(String regionId) {
        log.info("拿到的删除id是：{}",Long.parseLong(regionId));
        int i = regionMapper.selectRegionVMCountByRegionId(Long.parseLong(regionId));
        log.info("查到的关联的表数量为：{}",i);
        if (i>0){
            return false;
        }else{
            regionMapper.deleteRegion(regionId);
            return true;
        }




    }
}
